package com.woniuxy.charin.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.woniuxy.charin.mapper.TransactionOrderMapper;
import com.woniuxy.charin.model.entity.RechargeRecord;
import com.woniuxy.charin.model.entity.TransactionOrder;
import com.woniuxy.charin.model.vo.RechargeRecordVo;
import com.woniuxy.charin.service.RechargeRecordService;
import com.woniuxy.charin.mapper.RechargeRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【recharge_record】的数据库操作Service实现
* @createDate 2024-12-26 17:02:55
*/
@Service
public class RechargeRecordServiceImpl extends ServiceImpl<RechargeRecordMapper, RechargeRecord>
    implements RechargeRecordService{


    @Autowired
    private RechargeRecordMapper rechargeRecordMapper;

    @Autowired
    private TransactionOrderMapper transactionOrderMapper;

    @Override
    public Page<RechargeRecordVo> getRechargeRecords(Map<String, Object> queryParams, int pageNum, int pageSize) {
        Page<RechargeRecordVo> page = new Page<>(pageNum, pageSize);
        return rechargeRecordMapper.selectRechargeRecords(page, queryParams);
    }

    @Override
    public Page<RechargeRecordVo> getAllRechargeRecords(Page<RechargeRecordVo> page) {
        return rechargeRecordMapper.selectAllRechargeRecords(page);
    }

    @Override
    public RechargeRecordVo getRechargeRecordById(Long id) {
        return rechargeRecordMapper.selectRechargeRecordById(id);
    }

    // 生成充值订单号
    public String generateOrderNo() {
        return "CZ" + System.currentTimeMillis() + (int)(Math.random() * 1000);
    }


    @Transactional
    public void handleRecharge(Long customerId, BigDecimal amount, Long paymentId) {
        // 生成充值订单号
        String orderNo = generateOrderNo();

        // 插入充值记录
        RechargeRecord rechargeRecord = new RechargeRecord();
        rechargeRecord.setOrderNo(orderNo);
        rechargeRecord.setCustomerId(customerId);
        rechargeRecord.setRechargeAmount(amount);
        rechargeRecord.setPaymentId(paymentId);
        rechargeRecord.setTransactionStatus("已完成");
        rechargeRecord.setCreateTime(new Date());
        rechargeRecordMapper.insert(rechargeRecord);

        // 插入交易流水
        TransactionOrder transactionOrder = new TransactionOrder();
        transactionOrder.setOrderNo(orderNo);
        transactionOrder.setCustomerId(Math.toIntExact(customerId));
        transactionOrder.setTransactionType("充值");
        transactionOrder.setTransactionAmount(amount);
        transactionOrder.setPaymentMethodId(paymentId);
        transactionOrder.setStatus(1); // 已完成
        transactionOrder.setCreateTime(new Date());
        transactionOrderMapper.insert(transactionOrder);
    }




}




